package com.sls.nzdapplication.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.db.utils.EquipmentUtil;
import com.sls.nzdapplication.model.Equipment;
import com.sls.nzdapplication.model.ZyManagementEquipment;
import com.sls.nzdapplication.model.ZyManagementGt;
import com.sls.nzdapplication.ui.activity.EquipmentActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * ZyManagementGtFragment类是一个Fragment，用于展示某一条生产线上的所有设备的信息，包括设备名称、上一级设备名称、状态等。该类继承自Fragment类，实现了onCreateView()和onViewCreated()方法，以及initData()和showData()方法。
 *
 * 在onCreateView()方法中，该类加载了一个布局文件zy_gt_manage_layout.xml，并返回该布局文件的根视图。在onViewCreated()方法中，该类获取了传递过来的生产线ID，并初始化了一个SmartTable对象table，并对其进行了一些设置。然后调用了initData()方法，初始化了设备列表数据。
 *
 * initData()方法中，该类通过EquipmentUtil类查询了该生产线上的所有设备，并将查询结果保存在了equipmentList中。然后调用了showData()方法，展示设备列表数据。
 *
 * showData()方法中，该类首先清空了table的数据，然后遍历了titleData，即设备列表数据，将每个设备的信息封装成了一个ZyManagementGt对象，并将其添加到了zyManagementEquipments中。最后，如果zyManagementEquipments的大小大于0，则将table设置为可见，并将zyManagementEquipments设置为table的数据，并为table的每一行添加了点击事件，点击后跳转到EquipmentActivity页面，展示该设备的详细信息。如果zyManagementEquipments的大小等于0，则将table设置为不可见。
 *
 * 因此，ZyManagementGtFragment类主要实现了展示某一条生产线上的所有设备的信息的功能。
 *
 * Attempt Change
 * */
public class ZyManagementGtFragment extends Fragment {

    private EquipmentUtil equipmentUtil;

    private LinearLayout dataView;

    private List<Equipment> equipmentList;

    private String lineId;

    private SmartTable<ZyManagementGt> table;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.zy_gt_manage_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lineId = getArguments().getString("lineId");
        table = view.findViewById(R.id.table);
        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        table.getConfig().setShowTableTitle(false);
        table.getConfig().setMinTableWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth());
        table.getConfig().setColumnTitleStyle (new FontStyle(30, getResources().getColor(R.color.colorPrimaryDark)));
        initData();
    }

    private void initData(){
        equipmentUtil = new EquipmentUtil(getContext());
        equipmentList = equipmentUtil.queryEquipmentByLineIdAndGt(lineId);
        showData(equipmentList);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 数据
     */
    private void showData(List<Equipment> titleData) {
        table.setTableData(null);
        List<ZyManagementGt> zyManagementEquipments = new ArrayList<>();
        for (int i = 0; i < titleData.size(); i++) {
            final Equipment equipment = titleData.get(i);
            ZyManagementGt zyManagementEquipment = new ZyManagementGt();
            zyManagementEquipment.setXh(String.valueOf(i+1));
            zyManagementEquipment.setSbmc(equipment.getName());
            Equipment equipment1 = equipmentUtil.listOneEquipment(equipment.getPreviousEquipmentId());
            if(null!= equipment1){
                zyManagementEquipment.setSjmc(equipment1.getName());
            }else{
                zyManagementEquipment.setSjmc("");
            }
            zyManagementEquipment.setZt("正常");
            zyManagementEquipments.add(zyManagementEquipment);
        }

        if(zyManagementEquipments.size()>0){
            table.setVisibility(View.VISIBLE);
            table.setData(zyManagementEquipments);
            table.getTableData().setOnRowClickListener(new TableData.OnRowClickListener<ZyManagementGt>() {
                @Override
                public void onClick(Column column, ZyManagementGt zyManagementEquipment, int col, int row) {
                    Intent intent = new Intent(getActivity(), EquipmentActivity.class);
                    intent.putExtra("equipmentId",titleData.get(row).getId());
                    startActivity(intent);
                }
            });
        }else{
            table.setVisibility(View.GONE);
        }

    }
}
